package example.book.controller;

import example.book.model.BookInfos;
import example.book.model.PageRequest;
import example.book.server.BookServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
public class BookController {
    @Autowired
    BookServer bookServer;
    @RequestMapping("/getList")
    //前端发送翻页的信息到后端
    public List<BookInfos> getList(PageRequest pageRequest) {
        List<BookInfos> books = bookServer.getBookList(pageRequest);
        return books;
    }
    @RequestMapping("/addBook")
    public String addBook(BookInfos bookInfos) {
        log.info("添加图书信息",bookInfos);
        if(!StringUtils.hasLength(bookInfos.getBookName())
                ||!StringUtils.hasLength(bookInfos.getAuthor())
                ||bookInfos.getCount() == null
                ||bookInfos.getPrice() == null
                ||!StringUtils.hasLength(bookInfos.getPublish())
                ||bookInfos.getStatus() == null) {
            return "输入不合法，请重新输入。";
        }
        try {
            bookServer.addBook(bookInfos);
            return "";
        }catch (Exception e) {
            log.info("添加图书失败",e);
            return e.getMessage();
        }
    }
}
